package com.itheima.account.dao.impl;

import com.itheima.account.dao.AccountDao;
import com.itheima.account.pojo.Account;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

/**
 * @Description：查询所有用户
 */
public class AccountDaoImpl implements AccountDao {


    private JdbcTemplate jdbcTemplate;

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }


    /**
     *@Description 根据id查询账户信息
     *
     * @param id 用户id
     */
    @Override
    public Account findAccountById(Integer id) {
        String sql = "select * from account where id =?";
        return jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(Account.class), id);
    }


    /**
     * @Description 交易
     * @param id 交易账号Id
     * @param money 交易金额
     * @return
     */
    @Override
    public Integer dealMoney(Integer id, Float money) {
        //修改指定id账户的money金额，
        String sql = "update account set money = money + ? where id = ? and money+? >=0";
        return jdbcTemplate.update(sql, money, id, money);
    }


}